package com.lwq.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class XMLOper {
	private SAXBuilder builder = new SAXBuilder();
	private Document doc = null;
	private Element readRoot = null;
	/****************************************************************************
	 * read xml
	 ****************************************************************************/
	public Element readFromFile(String path)
	{
		File xmlFile = new File(path);
		if(xmlFile.exists())
		{
			try{
				FileInputStream inStream = new FileInputStream(xmlFile);		
				int fileLen = inStream.available();
				if (fileLen > 0)
				{
					builder = new SAXBuilder();
					doc = builder.build(inStream);
					if(doc != null)
					{
						readRoot = doc.getRootElement();
					}
				}
			}
			catch(Exception e)
			{
				readRoot = null;
				e.printStackTrace();
			}
		}
		return readRoot;
	}
	
	public Element readFromData(String xmldata)
	{
		ByteArrayInputStream byteinStream = null;
		byteinStream = new ByteArrayInputStream(xmldata.getBytes());
		try{
			builder = new SAXBuilder();
			doc = builder.build(byteinStream);
			if(doc != null)
			{
				readRoot = doc.getRootElement();
			}
		}
		catch(Exception e)
		{
			readRoot = null;
			e.printStackTrace();
		}
		return readRoot;
	}	
	
	
	public Element creatRootNode()
	{
		String inStr = "<root></root>";
		return readFromData(inStr);
	}
	public List getAllChildrenItems(Element point) {
		List list = null;
		if(point != null)
		{
			list = point.getChildren();
		}
		return list;
	}
	
	
	public Element getChildrenItem(Element point,String tagName) {
		Element child = null;
		if(point != null && !tagName.isEmpty())
		{
			child = point.getChild(tagName);
		}
		return child;
	}
	
	public int getIntValue(Element point,String tagName) 
	{
		int val = 0;
		if(point != null)
		{
			Element ele = point.getChild(tagName);
			if(ele != null)
			{
				val = Integer.parseInt(ele.getText());
			}
		}
		return val;
	}
	
	public String getStringValue(Element point,String tagName) 
	{
		String str = null;
		if(point != null)
		{
			Element ele = point.getChild(tagName);
			if(ele != null)
			{
				str = ele.getText();
			}
		}
		return str;
	}
	
	public int[] getIntArray(Element point,String tagName) {
		List list = null;
		int[] vals = null;
		Element ele = null;
		if(point != null)
		{
			list = point.getChildren(tagName);
			int len = list.size();
			vals = new int[len];
			for(int i=0;i<len;i++)
			{
				ele = (Element)list.get(i);
				String tmp = ele.getText();
				int num =  Integer.parseInt(tmp);
				vals[i] = num;
			}
		}
		return vals;
	}
	
	public String[] getStringArray(Element point,String tagName) {
		List list = null;
		String[] vals = null;
		Element target = null;
		Element ele = null;
		if(point != null)
		{
			target = point.getChild(tagName);
			list = target.getChildren();
			for(int i=0;i<list.size();i++)
			{
				ele = (Element)list.get(i);
				vals[i] = ele.getText();
			}
		}
		return vals;
	}

	/****************************************************************************
	 * write xml
	 ****************************************************************************/
	public boolean writeFile(Element root,String path)
	{
		boolean writeSuccess = false;
		if(!path.isEmpty())
		{
			try{
				XMLOutputter  XMLOut  =  new  XMLOutputter();
				XMLOut.output(doc, new FileOutputStream(path));
				writeSuccess = true;
			}
			catch(Exception e)
			{
				e.printStackTrace();
				writeSuccess = false;
			}
		}
		return writeSuccess;
	}
	
	
	public boolean removeNode(Element point,String tagName){
		boolean state = false;
		if(point != null)
		{
			state = point.removeChild(tagName);
		}
		return state;
	}
	
	public boolean putValue(Element point,String tagName,Object value) {
		Element target = point.getChild(tagName);
		boolean state = false;
		if(target != null)
		{
			if(!tagName.isEmpty())
			{
				target.setName(tagName);
			}
			target.setText(value.toString());
			state = true;
		}
		else
		{
			Element ele = new Element(tagName);
			ele.setText(value.toString());
			point.addContent(ele);
			state = true;
		}
		return state;
	}
	public boolean putArray(Element point,String tagName,List ls) {
		boolean state = false;
		if(point != null && ls != null && !tagName.isEmpty())
		{
			int len = ls.size();
			for(int i=0;i<len;i++){
				Element ele = new Element(tagName);
				String str = ls.get(i).toString();
				if(str != null)
				{
					ele.setText(str);
				}
				point.addContent(ele);
				state = true;
			}
		}
		return state;
	}
}
